package com.esd.device.controller;

import com.esd.common.pojo.R;
import com.esd.device.entity.dto.DeviceDto;
import com.esd.device.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/device")
public class DeviceController {

    @Autowired
    private DeviceService deviceService ;


    /**
     * 添加终端
     * @param deviceDto 终端实体
     * @return R
     */
    @PostMapping("addDevice")
    public R addDevice(@RequestBody DeviceDto deviceDto){
        return deviceService.addDevice(deviceDto);
    }

    /**
     * 修改终端信息
     * @param deviceDto 终端实体
     * @return R
     */
    @PostMapping("updateDevice")
    public R updateDevice(
            @RequestBody DeviceDto deviceDto
    ){
        return deviceService.updateDevice(deviceDto) ;
    }

    // 批量删除接口
    @PostMapping("batchDelete")
    public R batchDelete(
           @RequestBody List<Long> ids
    ){
        return deviceService.batchDelete(ids);
    }

    /**
     * 删除终端
     * @param deviceId Id
     * @return R
     */
    @DeleteMapping("deleteDevice/{deviceId}")
    public R deleteDevice(@PathVariable Long deviceId){
        return deviceService.deleteDevice(deviceId) ;
    }

    /**
     * 根据条件分页查询终端
     * @param params 查询条件实体
     * @return R
     */
    @PostMapping("selectDevicePage")
    public R selectDevicePage(@RequestBody Map<String,Object> params) {
        return deviceService.selectDevicePage(params);
    }

    // 使用文件上传的方式导入终端信息
    @PostMapping("uploadDeviceData")
    public R uploadDeviceData(
            @RequestParam("file") MultipartFile file
    ) throws IOException {
        return R.ok(deviceService.uploadDeviceData(file));
    }

    /**
     * 根据终端Id查询信息
     * @param deviceId deviceId
     * @return R
     */
    @GetMapping("selectById/{deviceId}")
    public R selectById(@PathVariable Long deviceId){
        DeviceDto deviceDto = deviceService.selectById(deviceId); // 需要修改
        return R.ok(deviceDto);
    }

    /**
     * 根据 line（产线）查询 终端列表 , 根据终端类型分组
     * @param lineId line
     * @return R
     */
    @GetMapping("selectByLine")
    public R selectByLineAndStation(
            @RequestParam(name = "lineId") Long lineId
    ){
        return deviceService.selectByLine(lineId) ;
    }

    @GetMapping("getByLineAndType")
    public R getByLineAndType(
            @RequestParam(name = "lineName") String lineName,
            @RequestParam(name = "deviceType") String deviceType
    ){
        return deviceService.getByLineAndType(lineName , deviceType);
    }

    @GetMapping("getCount")
    public Map<String , Integer> getCount(){
        return deviceService.getCount();
    }

    @GetMapping("getSortDeviceList/{lineId}")
    public R getSortDeviceList(@PathVariable Long lineId){
        List<HashMap<String , String>> devices = deviceService.getSortDeviceList(lineId) ;
        return R.ok(devices);
    }

    @GetMapping("sortDevice/{stationId1}/{stationId2}")
    public R sortDevice(
            @PathVariable String stationId1,
            @PathVariable String stationId2
    ){
        return deviceService.sortDevice(stationId1 , stationId2) ;
    }

    @GetMapping("/enabledMon/{id}/{value}")
    public R enabledMon(
            @PathVariable Long id ,
            @PathVariable String value
    ){
        return deviceService.enabledMon(id , value) ;
    }

    @PostMapping("getLinesData")
    public R getLinesData(@RequestBody Long [] ids){
        return deviceService.getLinesData(ids);
    }

    @GetMapping("/getVoById/{deviceId}")
    public R getDeviceVoByDeviceId(@PathVariable Long deviceId){
        return deviceService.getDeviceVoByDeviceId(deviceId);
    }

}
